
const wsp = {
  numToHex(v, n = 16) {
    return ('00' + v.toString(n)).slice(-2)
  },
  wspValueTransition(data, hbit, lbit, v = 1) {
    let valueL = this.numToHex(data[lbit])
    let valueH = this.numToHex(data[hbit])

    return +(+`0x${valueH}${valueL}` * v).toFixed(2)
  },
  wspWeigthTransition(v) {
    return +(v * 0.05).toFixed(2)
  },
  measureResult: null,
  wspMeasureFinish: false,
  wspMeasure(data, _scale) { //测量回调
    this.parentObj = _scale
    // console.log('_scale', _scale)
    if (!this.wspMeasureFinish) {
      let weight = this.wspValueTransition(data, 2, 1)

      weight = this.wspWeigthTransition(weight)

      _scale.that.onWeight(weight)
      this.wspMeasurePackage = []
      this.wspWeight = weight
      if (data[0] >> 7 & 1) {//心率测量中
        _scale.that.onStartFetch()
      } else if (data[0] >> 5 & 1) {//测量完成
        this.wspMeasureFinish = true
        this.wspMeasureEnd(data)
      }
    } else {
      this.wspMeasurePackage.push(data)
      if (this.wspMeasurePackage.length >= 2 && this.wspMeasureFinish) {
        // console.log('wsp 数据接收完成', this.wspMeasurePackage)
        console.log('wsp 数据接收完成')
        this.wspPackageTranstion()
        this.wspMeasureFinish = false
        this.wspMeasurePackage = []
      }
    }
  },
  wspPackageTranstion() {//解析数据包数据
    let data1 = this.wspMeasurePackage[0]
    let data2 = this.wspMeasurePackage[1]

    let result = {
      bodyfat: this.wspValueTransition(data1, 3, 2, 0.1),
      bmr: this.wspValueTransition(data1, 13, 12) / 10,
      muscle: this.wspValueTransition(data1, 15, 14, 0.1),
      sinew: this.wspValueTransition(data1, 17, 16, 0.05),
      fat_free_weight: this.wspValueTransition(data1, 19, 18, 0.05),

      water: this.wspValueTransition(data2, 1, 0, 0.1),
      resistance: this.wspValueTransition(data2, 3, 2),
      second_resistance: this.wspValueTransition(data2, 5, 4),
      bone: this.wspValueTransition(data2, 7, 6, 0.05) * 10,
      visfat: data2[8],
      subfat: this.wspValueTransition(data2, 10, 9, 0.1),
      protein: this.wspValueTransition(data2, 12, 11, 0.1),
      bodyage: data2[13],
      heart_rate: data2[14],
      score: this.wspValueTransition(data2, 16, 15, 0.1),
      body_shape: data2[17],
      cardiac_index: +(data2[18] * 0.1).toFixed(1),

    }
    Object.assign(this.measureResult, result)
    console.log('wspPackageTranstion result', this.measureResult)

    this.parentObj.that.onWspMeasureEnd(this.measureResult)
  },
  wspMeasureEnd(data) {
    if(!data){
      console.error('无data数据')
      return
    }
    let time = this.wspResultTime(data)
    let wspBMI = this.wspValueTransition(data, 12, 11, 0.1)

    let measureResult = {
      weight: this.wspWeight,
      bmi: wspBMI,
      local_updated_at: time.str
    }
    console.log('wsp measureResult', measureResult)
    this.measureResult = measureResult
  },
  wspResultTime(data) {//解析测量时间
    let year = +`0x${this.numToHex(data[4]) + this.numToHex(data[3])}`
    let month = this.numToHex(data[5], 10)
    let day = this.numToHex(data[6], 10)
    let hour = this.numToHex(data[7], 10)
    let min = this.numToHex(data[8], 10)
    let sec = this.numToHex(data[9], 10)
    let timestamp = Date.UTC(year, month - 1, day, hour, min, sec)
    // let timestamp = +new Date(year, month - 1, day, hour, min, sec)

    if (!(data[0] >> 2 & 1)) {//识别不出时间，取当前时间
      timestamp = +new Date
    }
    let d = new Date(timestamp)

    timestamp = parseInt(timestamp / 1000)

    year = d.getFullYear()
    month = this.numToHex(d.getMonth() + 1, 10)
    day = this.numToHex(d.getDate(), 10)
    hour = this.numToHex(d.getHours(), 10)
    min = this.numToHex(d.getMinutes(), 10)
    sec = this.numToHex(d.getMilliseconds(), 10)

    return {
      str: `${year}-${month}-${day} ${hour}:${min}:${sec}`,
      year,
      month,
      day,
      hour,
      min,
      sec,
      timestamp
    }
  },
}

module.exports = wsp